package com.happyfamily.springboot.crowdsourcingplatform.controller.pan;

import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.PageTableRequest;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.Orders;
import com.happyfamily.springboot.crowdsourcingplatform.model.Work;
import com.happyfamily.springboot.crowdsourcingplatform.service.pan.OrderService;
import com.happyfamily.springboot.crowdsourcingplatform.service.pan.PurchaseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author Administrator
 * @create 2020/02/28 00:00
 */
@Controller
@RequestMapping("/api-front/")
public class OrderController
{
	@Autowired
	private OrderService orderService;


	@Autowired
	private PurchaseService purchaseService;


	/**
	 * //跳转到雇主订单页面
	 * @return
	 */
	@RequestMapping("playOrder")
	public String userOrder(){
		return "frontPage/list-order-work";
	}





	@GetMapping("orderList")
	@ResponseBody
	@ApiOperation(value = "订单类型获取", notes = "查询类型是用户账号", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "订单类型获取", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public Results<Orders> getAllNeedsByPage(PageTableRequest request, HttpServletRequest httpServletRequest)
	{
		request.countOffset();
		SecurityContextImpl securityContextImpl = (SecurityContextImpl) httpServletRequest.getSession().getAttribute("SPRING_SECURITY_CONTEXT");

		//获得你登录的账号
		String account = securityContextImpl.getAuthentication().getName();

		return orderService.getOrderListByPage(request.getOffset(), request.getLimit(),account);
	}



	/**
	 * 跳转到下单详情页面
	 * @param model
	 * @param workid
	 * @return
	 */
	@GetMapping("userOrder")
	public String userDetail(Model model, String workid)
	{
		model.addAttribute("work", purchaseService.getWorkById(Integer.valueOf(workid)));
		return "frontPage/user-order";
	}

}
